<script setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';

// 模拟数据
const registrations = ref([]);
const searchParams = reactive({
  student: '',
  course: ''
});
const currentPage = ref(1);
const pageSize = ref(5);
const totalRegistrations = ref(0);

const fetchRegistrations = () => {
  registrations.value = Array.from({ length: 20 }, (_, i) => ({
    id: i + 1,
    student: `学生 ${i + 1}`,
    course: `课程 ${i + 1}`,
    created_at: `2025-03-${(i % 28) + 1}`,
  })).slice((currentPage.value - 1) * pageSize.value, currentPage.value * pageSize.value);
  totalRegistrations.value = 20;
};

const searchRegistrations = () => {
  ElMessage.success(`搜索：学生：${searchParams.student || '全部'}，课程：${searchParams.course || '全部'}`);
  fetchRegistrations();
};

const deleteRegistration = (id) => {
  ElMessageBox.confirm('确认删除该报名信息？', '提示', { type: 'warning' })
    .then(() => {
      registrations.value = registrations.value.filter(reg => reg.id !== id);
      ElMessage.success('删除成功');
    })
    .catch(() => ElMessage.info('取消删除'));
};

const handlePageChange = (page) => {
  currentPage.value = page;
  fetchRegistrations();
};

onMounted(() => {
  fetchRegistrations();
});
</script>

<template>
  <div class="page">
    <el-card class="search-box">
      <el-form :inline="true" class="search-form">
        <el-form-item label="学生">
          <el-input v-model="searchParams.student" placeholder="请输入学生姓名" clearable />
        </el-form-item>
        <el-form-item label="课程">
          <el-input v-model="searchParams.course" placeholder="请输入课程名称" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchRegistrations" icon="el-icon-search">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-button type="success" class="add-button">➕ 添加报名信息</el-button>

    <el-table :data="registrations" border>
      <el-table-column prop="student" label="学生" />
      <el-table-column prop="course" label="课程" />
      <el-table-column prop="created_at" label="报名时间" width="150" />
      <el-table-column label="操作" width="180">
        <template #default="{ row }">
          <el-button type="primary" size="small" @click="editRegistration(row.id)">修改</el-button>
          <el-button type="danger" size="small" @click="deleteRegistration(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :total="totalRegistrations"
      :page-size="pageSize"
      @current-change="handlePageChange"
    />
  </div>
</template>

<style scoped>
.page {
  padding: 30px;
  background: #f7f7f7;
}

.search-box {
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 20px;
  margin-bottom: 20px;
}

.add-button {
  background-color: #67c23a;
  color: white;
  border-radius: 8px;
  font-size: 14px;
}

.el-button {
  margin: 0 5px;
}

.el-table {
  margin-top: 20px;
}

.el-pagination {
  margin-top: 20px;
  text-align: center;
}
</style>
